package com.example.myandroidapp

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.example.myandroidapp.http.RetrofitClient
import com.example.myandroidapp.model.LoginRequest
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class RetrofitActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_retrofit)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        // 1. 获取频道列表
        findViewById<Button>(R.id.getChannel).setOnClickListener {
            try {
                // 启动一个新的协程，用于处理获取频道列表的异步网络请求
                lifecycleScope.launch(context = Dispatchers.Main) {
                    // 使用 withContext 切换到 IO 线程，调用 Retrofit 接口获取频道列表数据
                    val res = withContext(Dispatchers.IO) {
                        // 调用 RetrofitClient 的 ApiService 获取频道数据
                        RetrofitClient.apiService.getChannels()
                    }
                    if (res.code == 10000) {
                        // 处理响应
                        val data = res.data?.channels
                        findViewById<TextView>(R.id.channel).text =
                            "获取频道列表成功, 频道名称: ${data?.joinToString(", ") { it.name }}"
                    } else {
                        // 处理响应
                        Toast.makeText(
                            this@RetrofitActivity,
                            "获取频道列表失败",
                            Toast.LENGTH_SHORT
                        )
                            .show()
                    }
                }
            } catch (e: Exception) {
                // 处理错误
                Toast.makeText(
                    this@RetrofitActivity,
                    "请求失败",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }


        // 2. 登录操作
        findViewById<Button>(R.id.login).setOnClickListener {
            try {
                lifecycleScope.launch {
                    val req = LoginRequest("admin", "123456")
                    val res = withContext(Dispatchers.IO) {
                        RetrofitClient.apiService.login(req)
                    }

                    if (res.code == 10000) {
                        val token = res.data?.token
                        findViewById<TextView>(R.id.tv).text = "登录成功, Token: $token"
                    } else {
                        // 处理响应
                        Toast.makeText(
                            this@RetrofitActivity,
                            "登录失败",
                            Toast.LENGTH_SHORT
                        ).show()
                    }

                }
            } catch (e: Exception) {
                // 处理错误
                Toast.makeText(
                    this@RetrofitActivity,
                    "请求失败",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
    }
}